import Vue from "vue"
import Vuex from "vuex"
import common from "./modules/common"
import LoginModule from "./modules/LoginModule"
import api from "@/api/api.js"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userinfo: {
      username: "",
      token: ""
    },
    menus: [],
    addedRoutes: false
  },
  getters: {},
  mutations: {
    setUserInfo(state, payload) {
      state.userinfo = payload
    },
    clearUser(state) {
      state.setUserInfo = {
        username: "",
        token: ""
      }
    },
    setMenus: (state, menus) => {
      state.menus = menus
    },
    setAddedRoutes: (state, status) => {
      state.addedRoutes = status
    }
  },
  actions: {
    async toLogin({ commit }, params) {
      console("aaaaaaaaaaaaaaaaa")
    },
    getUserMenus({ commit, state }) {
      return new Promise((resolve, reject) => {
        api
          .post("/sys/getMuneTree")
          .then(response => {
            const menus = response.data.muneTreeList
            console.info(menus)
            commit("setMenus", menus)
            sessionStorage.setItem("dynamic_menus", JSON.stringify(menus))
            resolve(menus)
          })
          .catch(error => {
            commit("setMenus", [])
            sessionStorage.setItem("dynamic_menus", "[]")

            reject(error)
          })
      })
    }
  },
  modules: {
    common,
    LoginModule
  }
})
